package com.scs.application.modules.fsd.controller;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.entity.FsdUserRole;
import com.scs.application.modules.fsd.service.UserRoleService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description: 用户权限
 */
@RestController
@RequestMapping("/back_front/users_roles")
public class UserRoleController extends CrudController<UserRoleService, FsdUserRole> {

    @Autowired
    UserRoleService userRoleService;

    @GetMapping(value = "/getRolesByUserService")
    public RestResult getRolesByUserId(@RequestParam("id") String id){
        return RestResult.ok("获取该用户所拥有的权限成功",service.getRolesByUserId(id));
    }

    @GetMapping(value = "/getUsersByRoleService")
    public RestResult getUsersByRole(@RequestParam("id") String id){
        return RestResult.ok("获取该权限所拥有的用户成功",service.getUsersByRole(id));
    }

    @PostMapping(value = "/changeRolesByUserService")
    public RestResult saveUserRole(@RequestBody Map<String, String> map){
        if(userRoleService.saveByUsers(StringUtils.stringArryExtract(map.get("idAry"),"roleId"),map.get("id")))
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败！");
    }
    @PostMapping(value = "/changeUsersByRoleService")
    public RestResult changeUsersByRole(@RequestBody Map<String, String> map){
        if(service.changeUsersByRole(StringUtils.stringArryExtract(map.get("idAry"),"userId"),map.get("id")))
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败！");
    }

}
